﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tisbi.LawFirm.BL;
using Tisbi.LawFirm.BL.Services;
using Tisbi.LawFirm.DAL.Models;
using Tisbi.LawFirm.UI.Views.Controls.Forms;

namespace Tisbi.LawFirm.UI.ViewModel
{
    public class ClaimsViewModel : BaseViewModel<Claims, IServiceAgentBase<Claims>>
    {
        private ObservableCollection<Customers> _customers;
        public ObservableCollection<Customers> Customers
        {
            get { return _customers; }
            set
            {
                _customers = value;
                RaisePropertyChanged(() => Customers);
            }
        }

        protected override void LoadModelBySearch()
        {
            IsBusy = true;

            ModelServiceAgent.GetItem
                (
                    (list, exception) =>
                        ModelLoadedByWhere(list, exception, item => item.NumberOfNotice.Contains(SearchCriteria))
                );
        }

        protected override void ShowEditDialog()
        {
            if (SelectedModelItem == null)
            {
                return;
            }
            LoadData();

            var model = new ClaimsCRUDViewModel(SelectedModelItem) { Creators = Customers };
            ShowEditDialog<ClaimsCRUDForm>(model, "Карточка изменения претензии");
        }

        protected override void ShowCreatedDialog()
        {
            LoadData();

            Employees employe = SecurityContextSingleton.GetInstance().CurrentEmploye;
            var newItem = new Claims {Created = DateTime.Now, Employees = employe};
            var detailModel = new ClaimsCRUDViewModel(newItem) {Creators = Customers, AssingnCustomers = Customers};

            ShowCreatedDialog<ClaimsCRUDForm>(detailModel, "Карточка создания новой претензии");
        }

        private void LoadData()
        {
            
            IsBusy = true;

            CustomersServiceAgent.GetItem
                (
                    CustomersLoaded
                );
        }

        private void CustomersLoaded(List<Customers> entities, Exception error)
        {
            // If no error is returned, set the model to entities
            if (error == null)
            {
                Customers = new ObservableCollection<Customers>(entities);
            }
            // Otherwise notify view that there's an error
            else
            {
                Notifyer.NotifyError("Unable to retrieve items", error);
            }

            // Set SelectedItem to the first item
            if (Customers == null || Customers.Count <= 0)
            {
                HasVarnings = true;

                if (!string.IsNullOrWhiteSpace(Varnings))
                {
                    Varnings += "\n";
                }
                Varnings += "В базе отсудствуют клиенты, для продолжения работы добавьте в реестр нового [url=/Views/Pages/Customers.xaml|_top]клиента[/url].";
            }

            // We're done
            IsBusy = false;

            SetCanProperties();
        }
    }
}
